package Draw0513;

import Draw0512.Shape;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.util.ArrayList;

public class DrawListenerV2 implements ActionListener, MouseListener {
    JFrame jf ;
    Graphics g;
    int x1,x2,x3,x4,x5;
    int y1,y2,y3,y4,y5;
    int count = 0;
    String type = "直线";
    Color color = Color.BLACK;

    ShapeV2[] shapeV2List = new ShapeV2[100];
    int shapeV2Size = 0;
    ArrayList<Point> points = new ArrayList<>();



    @Override
    public void actionPerformed(ActionEvent e) {
        String ac = e.getActionCommand();
        switch (ac) {
            case "" -> {
                JButton btn = (JButton) e.getSource();
                color = btn.getBackground();
            }
            case "保存" -> {

            }
            case "撤销" -> {

            }
            default -> type = ac;
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        BufferedImage img = new BufferedImage(800, 700, 2);
        Graphics bg = img.getGraphics();// 绘制到图片上的画笔
        switch (type) {
            case "三角形" -> {
                if (count == 0) {
                    points.add(new Point(x, y));
                    count++;
                } else if (count == 1) {
                    points.add(new Point(x, y));
                    PolygonShape polygonShape = new PolygonShape(points.get(0), points.get(1),color);
                    polygonShape.drawShapeV2(bg);
                    count++;
                } else if (count == 2) {
                    points.add(new Point(x, y));
                    PolygonShape polygonShape = new PolygonShape(points.get(0), points.get(1), points.get(2),color);
                    polygonShape.drawShapeV2(bg);
                    g.drawImage(img,0,0,null);
                    shapeV2List[shapeV2Size++] = polygonShape;
                    count = 0;
                    points.clear();
                }
            }
            case "多边形" -> {
                if (count == 0) {
                    x3 = x;
                    y3 = y;
                    count++;
                } else if (count == 1) {
                    x4 = x;
                    y4 = y;
                    ShapeV2 shapeV2 = new ShapeV2(x3, y3, x4, y4, color, "直线");
                    shapeV2.drawShapeV2(bg);
                    shapeV2List[shapeV2Size++] = shapeV2;
                    count++;
                } else if (count == 2) {
                    x5 = x;
                    y5 = y;
                    if (Math.abs(x5 - x3) < 10 && Math.abs(y5 - y3) < 10) {
                        ShapeV2 shapeV2 = new ShapeV2(x3, y3, x4, y4, color, "直线");
                        shapeV2.drawShapeV2(bg);
                        shapeV2List[shapeV2Size++] = shapeV2;
                        g.drawImage(img,0,0,null);
                        count = 0;
                        return;
                    }
                    ShapeV2 shapeV21 = new ShapeV2(x4, y4, x5, y5, color, "直线");
                    shapeV21.drawShapeV2(bg);
                    shapeV2List[shapeV2Size++] = shapeV21;
                    x4 = x5;
                    y4 = y5;
                }
            }
            case "谢尔宾斯基三角形" -> {
                if (count == 0) {
                    points.add(new Point(x, y));
                    x3 = x;
                    y3 = y;
                    count++;
                } else if (count == 1) {
                    points.add(new Point(x, y));
                    x4 = x;
                    y4 = y;
                    ShapeV2 shapeV2 = new ShapeV2(x3, y3, x4, y4, color, "直线");
                    shapeV2.drawShapeV2(bg);
                    shapeV2List[shapeV2Size++] = shapeV2;
                    count++;
                } else if (count == 2) {
                    x5 = x;
                    y5 = y;
                    ShapeV2 shapeV2 = new ShapeV2(x3, y3, x5, y5, color, "直线");
                    shapeV2.drawShapeV2(bg);
                    shapeV2List[shapeV2Size++] = shapeV2;
                    ShapeV2 shapeV21 = new ShapeV2(x4, y4, x5, y5, color, "直线");
                    shapeV21.drawShapeV2(bg);
                    shapeV2List[shapeV2Size++] = shapeV21;
                    count = 0;
                    SierpinskiShape sierpinskiShape = new SierpinskiShape(x3, y3, x4, y4, x5, y5, color, type);
                    sierpinskiShape.drawShapeV2(bg);
                    shapeV2List[shapeV2Size++] = sierpinskiShape;
                    g.drawImage(img,0,0,null);
                }
            }
            case "测试分形" -> {
                FractalShape fractalShape = new FractalShape(color);
                fractalShape.drawFractal(bg);
                shapeV2List[shapeV2Size++] = fractalShape;
                g.drawImage(img,0,0,null);
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        int a = e.getX();
        int b = e.getY();
        x1 = a;
        y1 = b;
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        int x2 = e.getX();
        int y2 = e.getY();
        BufferedImage img = new BufferedImage(jf.getWidth(), jf.getHeight(), 2);
        Graphics bg = img.getGraphics();// 绘制到图片上的画笔

        switch (type) {
            case "谢尔宾斯基矩形" -> {
                SierpinskiShape sierpinskiShape1 = new SierpinskiShape(x1, y1, x2, y2, color, type);
                sierpinskiShape1.drawShapeV2(bg);
                shapeV2List[shapeV2Size] = sierpinskiShape1;
                shapeV2Size++;
            }
            case "谢尔宾斯基圆形" -> {
                SierpinskiShape sierpinskiShape = new SierpinskiShape(x1, y1, x2, y2, color, type);
                sierpinskiShape.drawShapeV2(bg);
                shapeV2List[shapeV2Size] = sierpinskiShape;
                shapeV2Size++;
            }
            case "递归仿真峰线" -> {
                KLine kLine = new KLine(x1, y1, x2, y2, color, type);
                kLine.drawShapeV2(bg);
                shapeV2List[shapeV2Size++] = kLine;
            }
            default -> {
                ShapeV2 shapeV2 = new ShapeV2(x1, y1, x2, y2, color, type);
                shapeV2.drawShapeV2(bg);
                shapeV2List[shapeV2Size] = shapeV2;
                shapeV2Size++;
            }
        }
        g.drawImage(img,0,0,null);
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }



}
